package com.apexsoft.assets.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.assets.base.constants.WechatApiConstants;
import com.apexsoft.live.exception.AuthException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class WechatApiUtil {

    /**
     * @name 中文名称
     * @description 根据页面URL和页面ticket生成接入JS-SDK接入码
     * @time 创建时间:2018年7月23日19:58:05
     * @param url：接入js-sdk的页面地址
     * 			ticket：通过token生成的接入js-sdk的ticket
     * @return 请求返回接入js-sdk所需json对象
     * @author 朱浩
     * @history 修订历史（历次修订内容、修订人、修订时间等）
     */
    public static JSONObject getSignature(String url, String ticket) {
        JSONObject rul = new JSONObject();

        String noncestr = getRandomString(16);
        String timestamp = (int)(System.currentTimeMillis()/1000)+"";
        String sign = "";
        sign += WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_TICKET
                + WechatApiConstants.QYWX_EQUAL
                + ticket
                + WechatApiConstants.QYWX_AND
                + WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_NONCESTR
                + WechatApiConstants.QYWX_EQUAL
                + noncestr
                + WechatApiConstants.QYWX_AND
                + WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_TIMESTAMP
                + WechatApiConstants.QYWX_EQUAL + timestamp
                + WechatApiConstants.QYWX_AND
                + WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_URL
                + WechatApiConstants.QYWX_EQUAL + url;
        String signature = "";
        try {
            // 指定sha1算法
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(sign.getBytes());
            // 获取字节数组
            byte[] messageDigest = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            signature = hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        rul.put(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_SIGNATURE,
                signature);
        rul.put(WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_NONCESTR,
                noncestr);
        rul.put(WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_TIMESTAMP,
                timestamp);
        return rul;
    }

    /**
     * @name 中文名称
     * @description 根据扫码登录返回的code值和应用token值获取扫码登录人用户id(注：id为企业微信扫码人id)
     * @time 创建时间:2018年7月23日14:07:28
     * @param code
     *            :前台扫码登录后微信js插件会自动跳转链接，且附带该code。 accessToken: 应用调用接口凭证，
     *            该类下getTencentToken()方法可直接获取。
     * @return 请求返回userid
     * @author 朱浩
     * @history 修订历史（历次修订内容、修订人、修订时间等）
     */
    public static String getTencentUserInfo(String code, String accessToken) throws AuthException {
        String url = "";
        url += WechatApiConstants.QYWX_GET_USERINFO_URL
                + WechatApiConstants.QYWX_QUERY;
        url += WechatApiConstants.QYWX_GET_USERINFO_URL_PARAM_TOKEN
                + WechatApiConstants.QYWX_EQUAL + accessToken;
        url += WechatApiConstants.QYWX_AND;
        url += WechatApiConstants.QYWX_GET_USERINFO_URL_PARAM_CODE
                + WechatApiConstants.QYWX_EQUAL + code;
        JSONObject userInfoJson = sendPostRequest(url);
        String errcode = userInfoJson
                .getString(WechatApiConstants.QYWX_GET_USERINFO_RETURN_ERRCODE);
        if (WechatApiConstants.QYWX_GET_USERINFO_RETURN_SUCCESS_CODE
                .equals(errcode)) {
            return userInfoJson
                    .getString(WechatApiConstants.QYWX_GET_USERINFO_RETURN_USERID);
        } else {
            throw new AuthException(-Math.abs(userInfoJson.getIntValue(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRCODE)),
                    userInfoJson.getString(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRMSG));
        }
    }

    /**
     * @name 中文名称
     * @description 获取企业微信Token（应用调用接口凭证） 该值具有以下特性： 1、通用性：该企业微信应用下通用， 2、
     *              有效性：该值有效时长为7200秒， 3、微信端不可长期无限制请求，推荐使用缓存保存改值
     * @time 创建时间:2018年7月23日14:07:28
     * 请求URL地址，参数需自行拼接
     * @return 请求返回token值
     * @author 朱浩
     * @history 修订历史（历次修订内容、修订人、修订时间等）
     */
    public static String getTencentToken(String corpid, String secret) throws AuthException {
        String url = "";
        url += WechatApiConstants.QYWX_GET_TOKEN_URL
                + WechatApiConstants.QYWX_QUERY;
        url += WechatApiConstants.QYWX_GET_TOKEN_URL_PARAM_CORPID
                + WechatApiConstants.QYWX_EQUAL
                + corpid;
        url += WechatApiConstants.QYWX_AND
                + WechatApiConstants.QYWX_GET_TOKEN_URL_PARAM_CORPSECRET
                + WechatApiConstants.QYWX_EQUAL;
        url += secret;
        JSONObject tokenJson = sendPostRequest(url);
        String errcode = tokenJson
                .getString(WechatApiConstants.QYWX_GET_TOKEN_RETURN_ERRCODE);
        if (WechatApiConstants.QYWX_GET_TOKEN_RETURN_SUCCESS_CODE
                .equals(errcode)) {
            return tokenJson
                    .getString(WechatApiConstants.QYWX_GET_TOKEN_RETURN_TOKEN);
        } else {
            throw new AuthException(-Math.abs(tokenJson.getIntValue(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRCODE)),
                    tokenJson.getString(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRMSG));
        }
    }

    /**
     * @name 中文名称
     * @description 根据token获取接入js-sdk的ticket
     * @time 创建时间:2018年7月23日19:33:47
     * @param token 应用调用接口凭证， 该类下getTencentToken()方法可直接获取。
     * @return 请求返回ticket值。
     * @author 朱浩
     * @history 修订历史（历次修订内容、修订人、修订时间等）
     */
    public static String getTencentJSSDKTicket(String token) throws AuthException {
        String url = "";
        url += WechatApiConstants.QYWX_GET_JSAPITICKET_URL
                + WechatApiConstants.QYWX_QUERY;
        url += WechatApiConstants.QYWX_GET_JSAPITICKET_URL_PARAM_TOKEN
                + WechatApiConstants.QYWX_EQUAL + token;
        JSONObject ticketJson = sendPostRequest(url);
        String errcode = ticketJson
                .getString(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRCODE);
        if (WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_SUCCESS_CODE
                .equals(errcode)) {
            return ticketJson
                    .getString(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_TICKET);
        } else {
            throw new AuthException(-Math.abs(ticketJson.getIntValue(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRCODE)),
                    ticketJson.getString(WechatApiConstants.QYWX_GET_JSAPITICKET_RETURN_ERRMSG));
        }
    }

    /**
     * @name 中文名称
     * @description 相关说明
     * @time 创建时间:2018年7月23日11:48:33
     * @param url 请求URL地址，参数需自行拼接
     * @return 请求返回json对象
     * @author 朱浩
     * @history 修订历史（历次修订内容、修订人、修订时间等）
     */
    private static JSONObject sendPostRequest(String url) {
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            URL postUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) postUrl
                    .openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(stringBuffer.toString());
    }

    /**
     * @name 中文名称
     * @description 获取指定位数的随机字符串(包含小写字母、大写字母、数字,0<length)
     * @time 创建时间:2018年7月23日14:17:21
     * @param length 获取字符串长度
     * @return 对应长度的随机字符串
     * @author 朱浩
     * @history 修订历史（历次修订内容、修订人、修订时间等）
     */
    private static String getRandomString(int length) {
        // 随机字符串的随机字符库
        String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer sb = new StringBuffer();
        int len = KeyString.length();
        for (int i = 0; i < length; i++) {
            sb.append(KeyString.charAt((int) Math.round(Math.random()
                    * (len - 1))));
        }
        return sb.toString();
    }
}
